<template>
    <div class="index-containar">
        <div class="search">
            <div class="logo">
                <img src="../assets/images/logo.png" alt="" />
            </div>
            <van-search
                v-model="value"
                shape="round"
                placeholder="寻找宝贝店铺"
            />
        </div>
        <!-- 按照老师的优化v-for和懒加载 -->
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
            <van-swipe-item v-for="item in lunbo" :key="item.img"
                ><img v-lazy="item.img" alt="" />
            </van-swipe-item>
        </van-swipe>
        <!-- 九宫格优化 -->
        <van-grid square>
            <van-grid-item v-for="item in grid" :key="item.img" :to="item.to">
                <div class="item">
                    <img :src="item.img" alt="" />
                    <div class="text">{{ item.text }}</div>
                </div>
            </van-grid-item>
        </van-grid>

        <van-divider
            :style="{
                color: '#1989fa',
                borderColor: '#1989fa',
                padding: '0 16px',
                margin: '4px auto',
            }"
            >为你推荐</van-divider
        >
        <div class="goodslist">
            <!-- <router-link tag="div" :to="'/Goodsdetail/'+item.id" class="item" v-for="item in goods" :key="item.id">
                <div class="imgWrap flex_c_c">
                    <img v-lazy="item.img_url" alt />
                </div>
                <div class="title ellipsis_l1">{{ item.title }}</div>
                <div class="price">
                    <span class="yuan">&yen; {{ item.sell_price }}</span>
                    <span class="buy">{{ item.buy }}购买</span>
                </div>
            </router-link> -->
            <Goods
                v-for="item in goods"
                :key="item.id"
                :data="item"
                @click="handle"
            ></Goods>
        </div>
        <BackTop :top="500">顶部</BackTop>
    </div>
</template>

<script>
import Vue from "vue";
import { Lazyload } from "vant";
import Goods from "@/components/Goods.vue";
import BackTop from "@/components/BackTop.vue";
Vue.use(Lazyload);
// 导入轮播图接口
import { fetchLunbo, fetchRecommend } from "@/api/index";
export default {
    data() {
        return {
            value: "",
            lunbo: [],
            goods: [],
            grid: [
                {
                    text: "商品列表",
                    img: require("../assets/images/menu10.png"),
                    to: "/Goodslist",
                },
                {
                    text: "新闻列表",
                    img: require("../assets/images/menu19.png"),
                    to: "/Newslist",
                },
                {
                    text: "美图欣赏",
                    img: require("../assets/images/menu18.png"),
                    to: "/Goodslist",
                },
                {
                    text: "分类",
                    img: require("../assets/images/menu12.png"),
                    to: "/Goodslist",
                },
                {
                    text: "9.9拼单",
                    img: require("../assets/images/menu15.png"),
                    to: "/Goodslist",
                },
                {
                    text: "话费查询",
                    img: require("../assets/images/menu13.png"),
                    to: "/Goodslist",
                },
                {
                    text: "物流查询",
                    img: require("../assets/images/menu16.png"),
                    to: "/Goodslist",
                },
                {
                    text: "查看更多",
                    img: require("../assets/images/menu17.png"),
                    to: "/Goodslist",
                },
            ],
        };
    },
    created() {
        this._fetchLunbo();
        this._fetchRecommend();
    },
    methods: {
        async getLunbo() {
            // fetchLunbo()
            let result = await this.$httpGet(
                "http://api.w0824.com/api/getlunbo"
            );
            console.log(result);
        },
        async _fetchLunbo() {
            let { message } = await fetchLunbo();
            this.lunbo = message;
        },
        async _fetchRecommend() {
            let { message } = await fetchRecommend();
            this.goods = message;
        },
        handle(data, event) {
            this.$router.push("/goodsDetail/" + data.id);
        },
    },
    components: {
        Goods,
        BackTop,
    },
};
</script>

<style lang="scss" scoped>
.index-containar {
    min-width: 320px;
    max-width: 750px;
    margin: 0 auto;
    .search {
        position: sticky;
        z-index: 999;
        top: 0;
        left: 0;
        display: flex;
        align-items: center;
        background-color: #fff;
        .logo {
            width: 50px;
            height: 50px;
            background-color: #fff;
            img {
                width: 100%;
                height: 100%;
            }
        }
        .van-search {
            flex: 1;
        }
    }
    .my-swipe .van-swipe-item {
        color: skyblue;
        font-size: 20px;
        text-align: center;
        img {
            width: 100%;
            box-sizing: border-box;
        }
    }
    .van-grid {
        .item {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            font-size: 14px;
            img {
                width: 50%;
            }
            .text {
                color: #524949;
                font-size: 14px;
            }
        }
    }
    .goodslist {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
        background: skyblue;
        padding-top: 10px;

        .item {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            height: 200px;
            width: 47%;
            padding: 6px;
            border-radius: 8px;
            background: #fff;
            margin-bottom: 4px;
            font-size: 14px;
            .imgWrap {
                height: 124px;
                width: 100%;
                img {
                    height: 100%;
                    width: 100%;
                }
            }

            .price {
                display: flex;
                justify-content: space-between;
                align-items: center;

                .yuan {
                    color: red;
                    font-size: 16px;
                    font-weight: 700;
                }

                .buy {
                    font-size: 12px;
                    color: #999;
                }
            }
        }
    }
    .backtop {
        font-size: 12px;
    }
}
.gao {
    height: 1000px;
    background-color: blue;
}
</style>
